library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidytext)
library(tm)
## Loading required package: NLP
##
## Attaching package: 'NLP'
##
## The following object is masked from 'package:ggplot2':
##
## annotate
library(SnowballC)
library(Rtsne)
library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
library(scales)
##
## Attaching package: 'scales'
##
## The following object is masked from 'package:purrr':
##
## discard
##
## The following object is masked from 'package:readr':
##
## col_factor
library(wordcloud)
## Loading required package: RColorBrewer
library(viridis)
## Loading required package: viridisLite
##
## Attaching package: 'viridis'
##
## The following object is masked from 'package:scales':
##
## viridis_pal
library(DT)
library(plotly)
# Load and preprocess data
data = read_csv("https://raw.githubusercontent.com/JIHONGKING/Data_Analysis/refs/heads/main/carbon.csv") %>%
mutate(Year = strtoi(substring(Date, 7,10)))
## Rows: 5677 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Country, Region, Date
## dbl (2): Kilotons of Co2, Metric Tons Per Capita
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Summarize data by region and year
summary = data %>%
group_by(Region, Year) %>%
summarise(`Metric Tons Per Capita` = mean(`Metric Tons Per Capita`),
.groups = 'drop')
# Create improved visualization
p <- ggplot(summary) +
geom_line(aes(Year, `Metric Tons Per Capita`, color = Region), size = 1) +
scale_color_brewer(palette = "Set1") + # Better color palette for distinction
theme_minimal() +
labs(
title = 'Average Metric Tons per Capita of CO2',
subtitle = 'Separated by Region',
x = 'Year',
y = 'Metric Tons Per Capita',
color = 'Region'
) +
theme(
plot.title = element_text(face = "bold", size = 14),
axis.title = element_text(face = "bold"),
legend.position = "bottom",
panel.grid.minor = element_blank()
) +
# Add annotations for major trend changes
geom_point(data = subset(summary,
(Region == "Europe" & Year == 2008) |
(Region == "Asia" & Year == 2010)),
aes(Year, `Metric Tons Per Capita`, color = Region), size = 3)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# Convert to interactive plot with plotly (if desired)
ggplotly(p, tooltip = c("Year", "Metric Tons Per Capita", "Region"))